<?php
$menumark = 'order';
switch ($act) {
    //#####################@ 修改分类 @#####################//
    case 'order_detail':
        $order = $db->pe_select('order', array('order_id' => $_g_id));
        $house = $db->pe_select('house', array('house_id' => $order['order_houseid']), 'house_name');
        $order['house_name'] = $house['house_name'];
        //判断折扣类型
        if ($order['order_isvip'] == 1) {
            $order['sale_type'] = "会员卡";
            $sale = $db->pe_select('vipcard', array('vipcard_number' => $order['order_vnorcid']), "vipcard_sale");
            $order['sale'] = $sale['vipcard_sale'];
        } else {
            $order['sale_type'] = "无";
            $order['sale'] = "无";
        }


        //判断是支付类型
        switch ($order['order_payway']) {

            case'1':
                $order['order_payway'] = "线下支付";
                break;

            case'2':
                $order['order_payway'] = "储蓄卡支付";
                break;

            case'3':
                $order['order_payway'] = "微信支付";
                break;

            default:
                $order['order_payway'] = "未支付";
                break;
        }
        //预约人微信名
        $user = $db->pe_select('user', array('user_wxid' => $order['order_wxid']), "user_wxname");
        $order['order_wxid'] = $user['user_wxname'];

        include(pe_tpl('order_detail.html'));
        break;

    case 'add':

        if (isGet()) {


            include(pe_tpl('order_add.html'));
        } else if (isPost()) {


            $result = $db->pe_insert("order", $_p_info);
            if ($result) {
                pe_success('添加成功', 'admin.php?mod=order&act=add');
            } else {
                pe_error('添加失败');
            }

        }

        break;


    case 'del':

        if ($db->pe_delete('order', array('order_id' => $_g_id))) {
            pe_success('删除成功');
        } else {
            pe_error('删除失败');
        }

        break;

    case 'refund_apply':

        $order = $db->pe_selectall('order', array("order_ostate" => 4));
        foreach ($order as $k => $v) {
            $house = $db->pe_select('house', array('house_id' => $v['order_houseid']));
            $order[$k]['order_housename'] = $house['house_name'];
            $user = $db->pe_select('user', array('user_id' => $v['order_userid']));
            $order[$k]['user'] = $user['user_wxname'];
        }
        include(pe_tpl('refund_apply.html'));
        break;

    case 'acc_refund':
        require_once $pe['path_root'].'lib/WxPay.Api.php';

        $this_order = $db->pe_select('order', array('order_id' => $_g_id));
        $this_moneycard = $db->pe_select('moneycard', array('moneycard_uid' => $this_order['order_userid']));
        if($this_order['order_payway']==2){
            $moneycard = $db->pe_update('moneycard', array('moneycard_uid' => $this_order['order_userid']), array('moneycard_money' => $this_moneycard['moneycard_money'] +             $this_order['order_endprice']));

            $db->pe_update('order', array('order_id' => $_g_id), array('order_ostate' => 5));
            $order = $db->pe_selectall('order', array("order_ostate" => 4));
            pe_success('储蓄卡退款成功');
        }else{
            $_SESSION['refund_num'] = $this_order['order_refund_number'];
//           Header("Location: http://www.ktxwxgy.com/example/refund.php");
//     exit;


                $out_trade_no = $this_order['order_refund_number'];
                $total_fee = 1;
                $refund_fee = 1;
//
//                $total_fee = $this_order['order_endprice'];
//                $refund_fee = $this_order['order_endprice'];

                $input = new WxPayRefund();


                $input->SetOut_trade_no($out_trade_no);
                $input->SetTotal_fee($total_fee);
                $input->SetRefund_fee($refund_fee);
                $input->SetOut_refund_no(WxPayConfig::MCHID . date("YmdHis"));
                $input->SetOp_user_id(WxPayConfig::MCHID);

//            var_dump($input);
//            exit;

                $result = WxPayApi::refund($input);

                if(!empty($result)){
                    $db->pe_update('order', array('order_id' => $_g_id), array('order_ostate' => 5));
                    $order = $db->pe_selectall('order', array("order_ostate" => 4));
                    pe_success('微信退款成功','admin.php?mod=order&act=refund_apply');
                }




        }




        break;

    //核销订单，确定订单已使用
    case 'check':


        if ($db->pe_update('order', array('order_id' => $_g_id), array('order_state' => 1, 'order_ostate' => 2, 'order_usetime' => date('Y-m-d H-i-m', time())))) {
            pe_success('核销成功');
        } else {
            pe_error('核销失败');
        }


        break;


    case 'rej_refund':

        $db->pe_update('order', array('order_id' => $_g_id), array('order_ostate' => 1));
        $order = $db->pe_selectall('order', array("order_ostate" => 4));
        pe_error('拒绝退款成功');
        break;


    case 'cool':

        $db->pe_update('order', array('order_id' => $_g_id), array('order_ostate' => 6, 'order_delete' => 1));
        $order = $db->pe_selectall('order');
        pe_success('冻结订单成功');
        break;

//    case 'cool':
//
//        $db->pe_update('order', array('order_id' => $_g_id), array('order_ostate' => 6,'order_delete'=>1));
//        $order = $db->pe_selectall('order',array("order_ostate"=>4));
//        pe_error('冻结订单成功');
//        break;

    //#####################@ 分类列表 @#####################//
    default :
        if (isGet()) {
            $order = $db->pe_selectall('order', '', '*', array(10, $_g_page));
            foreach ($order as $k => $v) {
                $house = $db->pe_select('house', array('house_id' => $v['order_houseid']));
                $user = $db->pe_select('user', array('user_id' => $v['order_userid']));
                $order[$k]['order_housename'] = $house['house_name'];
                $order[$k]['user'] = $user['user_wxname'];
            }
        } else {
            $order_sql = "select * from wxgg_order where order_number like '%{$_p_order_number}%'";
            $order = $db->sql_selectall($order_sql);
        }


        include(pe_tpl('order_list.html'));

        break;
}
?><?php

